package com.wqtz.chechai.utils;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 10:56
 */

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtils {

	/**
	 * 保存路径的文件夹名称
	 */
	public static String DIR_NAME = "ballislove";

	/**
	 * 给指定的文件名按照时间命名
	 */
	private static SimpleDateFormat OUTGOING_DATE_FORMAT =
			new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS");


	/**
	 * 得到指定的Video保存路径
	 *
	 * @return
	 */
	public static File getDoneVideoPath(Context context) {
		String str = OUTGOING_DATE_FORMAT.format(new Date());
		File dir = new File(context.getFilesDir()
				+ File.separator + DIR_NAME + "/" + str);

		if (!dir.exists()) {
			dir.mkdirs();
		}

		return dir;
	}


	private static File getStorageDir(Context context) {
		// Get the directory for the app's private pictures directory.
		File file = new File(context.getExternalFilesDir(
				Environment.MEDIA_MOUNTED), "ZhouImage");
		if (!file.exists()) {
			if (!file.mkdirs()) {
				Log.e("TAG", "Directory not created");
			}
		}

		return file;
	}

	public static File getStorageDir2(Context context) {
		// Get the directory for the app's private pictures directory.
		File file = new File(Environment.getExternalStorageDirectory() + File.separator + "ZhouImage", OUTGOING_DATE_FORMAT.format(new Date()) + ".jpg");
		if (!file.exists()) {
			if (!file.mkdirs()) {
				Log.e("TAG", "Directory not created");
			}
		}

		return file;
	}

  /*  public static String getImageSavePath(){
        String filename=Environment.getExternalStorageDirectory()+File.separator+"ZhouImage", OUTGOING_DATE_FORMAT.format(new Date())+".jpg";
    }*/

	/**
	 * 得到输出的Video保存路径
	 *
	 * @return
	 */
	public static String newOutgoingFilePath(Context context) {
		String str = OUTGOING_DATE_FORMAT.format(new Date());
		String fileName = getStorageDir(context).getPath() + "/" + File.separator + str + ".jpg";
		return fileName;
	}

	/**
	 * 从asset路径下读取对应文件转String输出
	 *
	 * @param mContext
	 * @return
	 */
	public static String getJson(Context mContext, String fileName) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		AssetManager am = mContext.getAssets();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					am.open(fileName)));
			String next = "";
			while (null != (next = br.readLine())) {
				sb.append(next);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			sb.delete(0, sb.length());
		}
		return sb.toString().trim();
	}

	private static final String DB_PATH = "/data/data/com.wqtz.chechai/databases/";
	private static final String DB_NAME = "Province-db";

	/**
	 * 复制assets下数据库到data/data/packagename/databases
	 *
	 * @param context
	 * @throws IOException
	 */
	public static void copyDBToDatabases(Context context) throws IOException {
		String outFileName = DB_PATH + DB_NAME;

		File file = new File(DB_PATH);
		if (!file.mkdirs()) {
			file.mkdirs();
		}

		if (new File(outFileName).exists()) {
			// 数据库已经存在，无需复制
			return;
		}

		InputStream myInput = context.getAssets().open(DB_NAME);
		OutputStream myOutput = new FileOutputStream(outFileName);

		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}

		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	/*@复制asset文件到指定目录
	* @param oldPath  asset下的路径
	* @param newPath  SD卡下保存路径
	*/
	public static void CopyAssets(Context context, String oldPath, String newPath) {
		try {
			String fileNames[] = context.getAssets().list(oldPath);// 获取assets目录下的所有文件及目录名
			if (fileNames.length > 0) {// 如果是目录
				File file = new File(newPath);
				file.mkdirs();// 如果文件夹不存在，则递归
				for (String fileName : fileNames) {
					CopyAssets(context, oldPath + "/" + fileName, newPath + "/" + fileName);
				}
			} else {// 如果是文件
				InputStream is = context.getAssets().open(oldPath);
				FileOutputStream fos = new FileOutputStream(new File(newPath));
				byte[] buffer = new byte[1024];
				int byteCount = 0;
				while ((byteCount = is.read(buffer)) != -1) {// 循环从输入流读取
					// buffer字节
					fos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
				}
				fos.flush();// 刷新缓冲区
				is.close();
				fos.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
